package com.gdut.demo.controller;

import com.alibaba.fastjson.JSONArray;
import com.gdut.demo.entity.Ecg;
import com.gdut.demo.service.EcgService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@Controller
public class HistoryDataController {

    @Autowired
    private EcgService ecgService;

    /*
    * 修改默认启动页面
    * 只要修改 return "xxxx";
    * */
    @RequestMapping("/test")
    public String index() {
//        return "testGetAll";
        return "redirect:/testPageHelper";
    }


    @GetMapping("/testGetOnePageData")
    @ResponseBody
    public JSONArray getOnePageData(@RequestParam("page") Integer page) {
        //LIMIT参数：表示从第几行开始，默认第0行开始
        Integer startLine = 0;
        JSONArray jsonArray = new JSONArray();
        //如果前端传来的页码不是第一页就，按倍数来计算从哪一行开始
        if (page != 1) {
            startLine = (page-1) * 500;
        }
        //获得一页所需的数据
        List<Ecg> onePageDate = ecgService.getOnePageDate(startLine);

        //转为json数组
        for (int i = 0; i < onePageDate.size(); i++) {
            HashMap hashMap = new HashMap<String,String>();
            hashMap.put("id",onePageDate.get(i).getId());
            hashMap.put("voltage",onePageDate.get(i).getVoltage());
            jsonArray.add(hashMap);
        }
        System.out.println("当前页："+page);
        System.out.println("数据："+jsonArray);

        return jsonArray;
    }

    @GetMapping("/testPageHelper")
    public String testPageHelper(@RequestParam(defaultValue = "1") int pageNum,
                                 @RequestParam(defaultValue = "2500") int pageSize,
                                 HttpServletRequest request,
                                 Model model) {

        //创建json数组
        JSONArray jsonArray = new JSONArray();

        PageHelper.startPage(pageNum,pageSize);
        PageInfo pageInfo = new PageInfo(ecgService.getAllFromEcg8Hours());
//        PageInfo pageInfo = new PageInfo(ecgService.getAll());


        int pageNum1 = pageInfo.getPageNum();
        int prePage = pageInfo.getPrePage();
        int nextPage = pageInfo.getNextPage();

        List<Ecg> pagelist = pageInfo.getList();
        System.out.println(pagelist);
        for (int i = 0; i < pagelist.size(); i++) {
            HashMap hashMap = new HashMap<String,String>();
            hashMap.put("id",pagelist.get(i).getId());
            hashMap.put("voltage",pagelist.get(i).getVoltage());
            jsonArray.add(hashMap);
        }
        request.setAttribute("jsonArray",jsonArray);
        request.setAttribute("pageNum",pageNum1);
        request.setAttribute("prePage",prePage);
        request.setAttribute("nextPage",nextPage);

//        request.setAttribute("pageInfo",pageInfo);
//        System.out.println(jsonArray);
        return "demo-static";
    }


    /*不可行*/
    @GetMapping("/testPageHelper2")
    public String testPageHelper2(@RequestParam(defaultValue = "1") int pageNumForEcg,
                                  @RequestParam(defaultValue = "2500") int pageSizeForEcg,
                                 @RequestParam(defaultValue = "1") int pageNumForSpo2,
                                 @RequestParam(defaultValue = "2500") int pageSizeForSpo2,

                                  HttpServletRequest request,
                                 Model model) {

        //创建json数组
        JSONArray jsonArrayForEcg = new JSONArray();
        JSONArray jsonArrayForSpo2 = new JSONArray();

        PageHelper.startPage(pageNumForEcg,pageSizeForEcg);
        PageInfo pageInfoForEcg = new PageInfo(ecgService.getAllFromEcg8Hours());
//        PageInfo pageInfo = new PageInfo(ecgService.getAll());

        int pageNumForEcg1 = pageInfoForEcg.getPageNum();
        int prePageForEcg = pageInfoForEcg.getPrePage();
        int nextPageForEcg = pageInfoForEcg.getNextPage();

        List<Ecg> pagelistForEcg = pageInfoForEcg.getList();
        System.out.println(pagelistForEcg);
        for (int i = 0; i < pagelistForEcg.size(); i++) {
            HashMap hashMap = new HashMap<String,String>();
            hashMap.put("id",pagelistForEcg.get(i).getId());
            hashMap.put("voltage",pagelistForEcg.get(i).getVoltage());
            jsonArrayForEcg.add(hashMap);
        }

        PageHelper.startPage(pageNumForSpo2,pageSizeForSpo2);
        PageInfo pageInfoForSpo2 = new PageInfo(ecgService.getAllFromEcg8Hours());
//        PageInfo pageInfo = new PageInfo(ecgService.getAll());

        int pageNumForSpo2_1 = pageInfoForSpo2.getPageNum();
        int prePageForSpo2 = pageInfoForSpo2.getPrePage();
        int nextPageForSpo2 = pageInfoForSpo2.getNextPage();

        List<Ecg> pagelistForSpo2 = pageInfoForSpo2.getList();
        System.out.println(pagelistForSpo2);
        for (int i = 0; i < pagelistForSpo2.size(); i++) {
            HashMap hashMap = new HashMap<String,String>();
            hashMap.put("id",pagelistForSpo2.get(i).getId());
            hashMap.put("voltage",pagelistForSpo2.get(i).getVoltage());
            jsonArrayForEcg.add(hashMap);
        }

        request.setAttribute("jsonArrayForEcg",jsonArrayForEcg);
        request.setAttribute("pageNumForEcg",pageNumForEcg1);
        request.setAttribute("prePageForEcg",prePageForEcg);
        request.setAttribute("nextPageForEcg",nextPageForEcg);

//        request.setAttribute("jsonArray",jsonArray);
        request.setAttribute("pageNum",pageNumForEcg1);
        request.setAttribute("prePage",prePageForEcg);
        request.setAttribute("nextPage",nextPageForEcg);

//        request.setAttribute("pageInfo",pageInfo);
//        System.out.println(jsonArray);
        return "demo-static";
    }

}
